﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using Qiang.Topology.Geometries;

namespace Qiang.Data
{

    /// <summary>
    /// 面类
    /// </summary>]
    [Serializable]
    public class PolygonShape : Shape, IPolygonShape
    {
        # region 初始化

        //面的构成
        //面也是有方向的
       
        public PolygonShape(IPart shell)
            : base()
        {
            _shell = shell;
            _holes = new List<IPart>();
            ID = "多边形" + Count.ToString();
            Count++;
        }

        # endregion

        static int Count = 0;

        #region 属性

        /// <summary>
        /// shape类型.
        /// </summary>
        public override ShapeType ShapeType
        {
            get
            {
                return ShapeType.Polygon;
            }
        }
            

        private IPart _shell;

        /// <summary>
        /// 外环。
        /// </summary>
        public IPart Shell
        {
            get { return _shell; }
            set { _shell = value; }
        }


        private List<IPart> _holes;

        /// <summary>
        /// 洞
        /// </summary>
        public List<IPart> Holes
        {
            get { return _holes; }
            set { _holes = value; }
        }

      
       

        #endregion
              

        #region 方法

                
      

        #endregion


        #region 继承的方法

        /// <summary>
        /// 获取多边形的范围
        /// </summary>
        /// <returns></returns>
        protected override IExtent ComputeExtent()
        {
         

            IExtent ext = new Extent();
          

            return ext;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override IList<Coordinate> ComputeCoordinates()
        {
            List<Coordinate> coors = new List<Coordinate>();
            coors.AddRange(_shell.Coordinates);
            foreach (IPart prt in _holes)
            {
                coors.AddRange(prt.Coordinates);
            }
            return coors;
        }


        #endregion


        public override IGeometry ToGeometry()
        {
            if (_holes.Count == 0)
            {
                List<Coordinate> coors = new List<Coordinate>();
                coors.AddRange(Coordinates);
                coors.Add(coors[0]);
                return new Polygon(coors.ToArray());
            }

            ILinearRing shell = _shell.ToGeometry() as ILinearRing;            
            List<ILinearRing> holes = new List<ILinearRing>();
            for (int i = 0; i < _holes.Count; i++)
            {
                ILinearRing hole = _holes[i].ToGeometry() as ILinearRing;
                holes.Add(hole);
            }
            IPolygon polygon = new Polygon(shell, holes.ToArray());
            return polygon;
        }

    }
}
